/*
 * @Author: yangrongxin
 * @Date: 2021-08-10 09:58:21
 * @LastEditors: liubin
 * @LastEditTime: 2021-10-25 18:03:56
 */

import { useState, useReducer } from 'react';
import { View, ScrollView } from '@tarojs/components';
import { NavBarSearch, CustomTitle } from '@/components/index';
import { useSafeArea } from '@/hooks/index';
import Taro, { useDidShow } from '@tarojs/taro';
import { UserDataComfirm } from '@/pages/AssessmentDetail/components/index';
import PickerWapperEmpty from '@/components/PickerWapper/empty';
import { CHANNEL_TYPE } from '@/pages/My/compoents/MyAssessment/dataDictionary';
import { UserInfoCard, RecommandTest, PasswordPanel } from './components';
import { getHomeData } from './service';
import styles from './index.module.less';

function reducer(state, action) {
  if (action.type === 'update') {
    return {
      ...state,
      ...action.payload,
    };
  }
}

const Home = () => {
  const [refresherTrigger, setRefresherTrigger] = useState<boolean>(false);
  const [homeData, setHomeData] = useState({
    articleList: [], // 展示宣教的文章
    measureTableList: [],
    waitMeasureTableList: [], // 待测评列表
    waitSuccessEvaluation: 0, // 待完成测评数量
    evaluationSuccessTotal: 0, // 已测评总数
    articleTotal: 0, // 已购宣教数量
  });
  const [safeTop] = useSafeArea();
  // 存储当前消息确认的数据 展示对应的跳转路径以及跳转参试的存储
  const [state, dispatch] = useReducer(reducer, {
    isShowUserData: false, // 控制当前弹窗是不是展示
    measureTableId: '', // 控制当前跳转的测评id
    name: '', // 当前对应的测评名称
    measureTableRecordId: '', // 当前对应的量表记录id
    evaluationId: '', // 当前对应的测评id
    evaluationType: '', // 当前对应的测评类型
    hideValidPassWord: false, // 判断当前是不是展示 密码确认框
    validConfirmInfoFn: null, // 判断当前是不是要进行消息确认的方法
  });

  // 读取对应的数据 用于页面跳转
  const {
    isShowUserData,
    measureTableId,
    name,
    measureTableRecordId,
    evaluationId,
    evaluationType,
    hideValidPassWord,
    validConfirmInfoFn,
  } = state;

  console.log('hideValidPassWord', hideValidPassWord);

  const updateSetUserData = (payload: any) => {
    dispatch({
      type: 'update',
      payload,
    });
  };

  const fetchHomeData = () => {
    getHomeData().then((res: any = {}) => {
      setHomeData(res);
      setTimeout(() => {
        setRefresherTrigger(false);
      }, 500);
    });
  };

  useDidShow(() => {
    fetchHomeData();
  });

  const showPassword: boolean = isShowUserData && !hideValidPassWord;

  return (
    <View>
      {/* 顶部搜索栏 -- 会浮在最上层的部分 */}
      <View className={styles.fixedHeader}>
        <CustomTitle />
        <NavBarSearch
          isHome
          isNormal
        />
      </View>
      <ScrollView
        scrollY
        className={`hxtPublicWapper ${styles.containerWapper}`}
        refresherEnabled
        onRefresherRefresh={() => {
          setRefresherTrigger(true);
          fetchHomeData();
        }}
        refresherTriggered={refresherTrigger}
      >
        {/* 顶部搜索栏 -- 会被遮挡的部分 */}
        <View
          className={styles.topHeader}
          style={{
            marginTop: safeTop + 90,
          }}
        >
          <UserInfoCard {...homeData} />
        </View>
        {/* 展示主要的待测评内容以及其他 */}
        <View className={styles.container}>
          <RecommandTest
            waitSuccessEvaluation={homeData.waitSuccessEvaluation}
            measureTableList={homeData.measureTableList}
            waitMeasureTableList={homeData.waitMeasureTableList}
            articleList={homeData.articleList}
            updateSetUserData={updateSetUserData}
          />
        </View>
        {/* 团队 / 院内展示对应的消息确认窗口 */}
      </ScrollView>

      <UserDataComfirm
        isOpened={isShowUserData && hideValidPassWord}
        toPayFn={() => {
          Taro.navigateTo({
            url: `/EvaluationModule/pages/CheckboxAnwser/index?measureTableId=${measureTableId}&title=${name}&measureTableRecordId=${measureTableRecordId}&evaluationId=${evaluationId}&evaluationType=${evaluationType}`,
          });
        }}
        onClose={() => updateSetUserData({ isShowUserData: false })}
      />


      {/* 展示院内密码的弹窗 */}
      <PickerWapperEmpty
        isOpened={showPassword}
        hidePaddingBottom
        extendsContainerWapper={styles.extendsPickerWapper}
        onClose={() => updateSetUserData({ isShowUserData: false, validPassWord: false })}
      >
        {/* 展示密码的输入框 */}
        {showPassword && <PasswordPanel
          isOpened={showPassword}
          onFinish={validConfirmInfoFn}
          evaluationId={evaluationId}
          measureTableId={measureTableId}
          evaluationType={evaluationType}
          name={name}
          measureTableRecordId={measureTableRecordId}
        />}
      </PickerWapperEmpty>

    </View>
  );
};

export default Home;
